package com.thkj.modules.user.service.impl;

import java.util.Map;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.thkj.modules.user.entity.TblSecurityAdmin;
import com.thkj.modules.user.service.TblSecurityAdminService;

import dp.dao.PagingList;
import dp.service.BaseService;

@Service
public class TblSecurityAdminServiceImpl extends BaseService implements TblSecurityAdminService {

	public void insertTblSecurityAdmin(TblSecurityAdmin t) {
		dao.insert("com.thkj.mapper.TblSecurityAdmin.insert", t);
	}

	public Integer updateTblSecurityAdmin(TblSecurityAdmin t) {
		Integer row = dao.update("com.thkj.mapper.TblSecurityAdmin.update", t);
		return row;
	}

	public Integer deleteTblSecurityAdmin(String t) {
		return dao.update("com.thkj.mapper.TblSecurityAdmin.delete", t);
	}

	public TblSecurityAdmin getTblSecurityAdminById(String t) {
		return dao.queryForObject("com.thkj.mapper.TblSecurityAdmin.getById", t, TblSecurityAdmin.class);
	}

	// 通过登录账号获取账户信息
	public TblSecurityAdmin getByCode(String code) {
		return dao.queryForObject("com.thkj.mapper.TblSecurityAdmin.getByCode", code, TblSecurityAdmin.class);
	}

	// 验证密码
	public TblSecurityAdmin validatePassword(String code, String password) {
		if (StringUtils.isBlank(password)) {
			throw new IllegalArgumentException("密码不能为空");
		}
		TblSecurityAdmin rmPerson = this.getByCode(code);
		if (rmPerson == null) {
			return null;
		}
		if ("1".equals(rmPerson.getEnabled())) {
			String password_md5 = DigestUtils.md5Hex(password);
			return password_md5.equals(rmPerson.getPassword()) ? rmPerson : null;
		} else {
			throw new IllegalArgumentException("账户未启用");
		}
	}

	@Override
	public PagingList<Map> query(int pageIndex, int pageSize, Map<String, Object> param) {
		return dao.queryForPagingList("com.thkj.mapper.TblSecurityAdmin.query", param, pageIndex, pageSize, Map.class);
	}

	@Override
	public Integer queryCountByRoleId(String role_id) {
		return dao.queryForObject("com.thkj.mapper.TblSecurityAdmin.count", role_id, Integer.class);
	}

	@Override
	public Integer setAcount(TblSecurityAdmin t) {
		return dao.update("com.thkj.mapper.TblSecurityAdmin.setAcount", t);
	}

}